﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
namespace ChangDiTools.Demo {
    public class ThirdGlyph : Glyph {
        private Point3d pt;//存储自定义对象捕捉靶框的位置
        public override void SetLocation(Point3d point) {
            pt = point;
        }

        protected override void SubViewportDraw(ViewportDraw vd) {
            Point2d glyphSize = vd.Viewport.GetNumPixelsInUnitSquare(pt);
            double glyphHeight = CustomObjectSnapMode.GlyphSize / glyphSize.Y;
            Matrix3d d2w = vd.Viewport.EyeToWorldTransform;
            //创建一个由4点组成的数组，表示自定义靶框的矩形顶点，并将其从DCS坐标系转换到WCS坐标系
            Point3d[] pts ={new Point3d(pt.X+glyphHeight,pt.Y,pt.Z).TransformBy(d2w),
                new Point3d(pt.X,pt.Y+glyphHeight,pt.Z).TransformBy(d2w),
                new Point3d(pt.X-glyphHeight,pt.Y,pt.Z).TransformBy(d2w),
                new Point3d(pt.X,pt.Y-glyphHeight,pt.Z).TransformBy(d2w)};
            //绘制表示自定义靶框的矩形
            vd.Geometry.Polygon(new Point3dCollection(pts));
            //在自定义靶框矩形的下方绘制文字,方向沿X方向
            vd.Geometry.Text(pts[3].PolarPointsl(0, glyphHeight), Vector3d.ZAxis, Vector3d.XAxis, glyphHeight, 1.0, 0.0, "1/3");
        }
    }
}
